package com.woniuxy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.dao.CarMapper;
import com.woniuxy.pojo.Car;
import com.woniuxy.pojo.CarPic;
import com.woniuxy.service.CarService;
import com.woniuxy.util.DataGridView;
import com.woniuxy.vo.CarVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CarServiceImpl implements CarService{
    @Autowired
    private CarMapper carMapper;

    @Override
    public DataGridView getCarListByPage(CarVo carVo) {
        Page<Object> page= PageHelper.startPage(carVo.getPage(),carVo.getLimit());
        List<Car> carList=this.carMapper.selectCarByProperty(carVo);
        return new DataGridView(page.getTotal(),carList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addCar(Car car) {
        boolean flag=false;
        int count=this.carMapper.insertCar(car);
        if(count>0){
            flag=true;
        }
        return flag;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addCar45Pic(CarPic carPic) {
        boolean flag=false;
        int count=this.carMapper.insertCarPic45(carPic);
        if(count>0){
            flag=true;
        }
        return false;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addCarPosPic(CarPic carPic) {
        boolean flag=false;
        int count=this.carMapper.insertCarPicPos(carPic);
        if(count>0){
            flag=true;
        }
        return false;
    }

    @Override
    public boolean addCarBackPic(CarPic carPic) {
        boolean flag=false;
        int count=this.carMapper.insertCarPicBack(carPic);
        if(count>0){
            flag=true;
        }
        return false;
    }

    @Override
    public CarPic findCarPic45ByCid(int cid) {
        return carMapper.findCarPic45ByCid(cid);
    }

    @Override
    public CarPic findCarPicBackByCid(int cid) {
        return carMapper.findCarPicBackByCid(cid);
    }

    @Override
    public CarPic findCarPicPosByCid(int cid) {
        return carMapper.findCarPicPosByCid(cid);
    }

    @Override
    public boolean updateCarBackPic(CarPic carPic) {
        int res = carMapper.updateCarBackPic(carPic);
        if(res>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateCar45Pic(CarPic carPic) {
        int res = carMapper.updateCar45Pic(carPic);
        if(res>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateCarPic(CarPic carPic) {
        int res = carMapper.updateCarPic(carPic);
        if(res>0){
            return true;
        }
        return false;
    }

    @Override
    public CarPic findCarPic(CarPic carPic) {
        return carMapper.findCarPic(carPic);
    }

    @Override
    public boolean addCarPic(CarPic carPic) {
        boolean flag=false;
        int count=this.carMapper.insertCarPic(carPic);
        if(count>0){
            flag=true;
        }
        return false;
    }

    @Override
    public boolean updateCarPosPic(CarPic carPic) {
        int res = carMapper.updateCarPosPic(carPic);
        if(res>0){
            return true;
        }
        return false;
    }


    @Transactional
    @Override
    public void removeCar(int id) {
        this.carMapper.deleteByPrimaryKey(id);
    }
    @Transactional
    @Override
    public void removeBathCar(Integer[] ids) {
        if(ids.length>0){
            for(Integer id:ids){
                this.removeCar(id);
            }
        }
    }

    @Override
    public boolean update(Car car) {
        int res = carMapper.update(car);
        if(res>0){
            return true;
        }
        return false;
    }

    @Override
    public Car findCarByCarNo(String car_no) {
        return carMapper.findCarByCarNo(car_no);
    }

    @Override
    public List<CarPic> findCarPicByCid(int cid) {
        return carMapper.findCarPicByCid(cid);
    }
}
